package com.valveinspectionsys.rushrepair.activity;

import java.util.ArrayList;
import java.util.List;

import com.valveinspectionsys.R;
import com.valveinspectionsys.dbhelp.XmlNameHelper;
import com.valveinspectionsys.povo.DeviceVo;
import com.valveinspectionsys.povo.ListViewInfo;
import com.valveinspectionsys.povo.RepairVo;
import com.valveinspectionsys.routing.BaseAdapter.ListViewAdapter;
import com.valveinspectionsys.rushrepair.db.RushRepairDao;
import com.valveinspectionsys.util.Model;
import com.valveinspectionsys.util.SharedPreferenceUtil;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;

/**
 * 强修列表(显示强修信息)
 * @author King
 *
 */
public class RushRepairListView extends Activity implements OnScrollListener{
    public List<ListViewInfo> items = new ArrayList<ListViewInfo>();
    private ListView listView;
    private int visibleLastIndex = 0; // 最后的可视项索引
    private int visibleItemCount; // 当前窗口可见项总数
    private ListViewAdapter adapter;
    private View loadMoreView;
    private Button loadMoreButton;
    private Handler handler = new Handler();
    private int count = 0;// 总页数
  
    public List<RepairVo> list = null;//设备信息
    private int page = 1;// 当前页数
    private TextView textview;// 页数信息
    private EditText edit;// 输入页数
    private String waterid;
    public Button button2;// 上一页面;
    public Button button3;// 下一页
    public Model model;
   
    
    public RushRepairDao  dao;
    public SharedPreferenceUtil sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

	super.onCreate(savedInstanceState);
	setContentView(R.layout.listview);
	sp=new SharedPreferenceUtil(this);
	dao=new RushRepairDao(this, sp.getValue(XmlNameHelper.SqliteName));
	
	loadMoreView = getLayoutInflater().inflate(R.layout.listview_button, null);
	loadMoreButton = (Button) loadMoreView.findViewById(R.id.listview_ok);
	button2 = (Button) loadMoreView.findViewById(R.id.listview_lastpage);
	button3 = (Button) loadMoreView.findViewById(R.id.listview_nextpage);
	button3.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View v) {

		// TODO Auto-generated method stub
		button3.setText("正在加载..."); // 设置按钮文字loading
		page++;
		if (page > count) {
		    page = count;
		    button3.setText("下一页");

		    return;
		}
		handler.postDelayed(new Runnable() {
		    @Override
		    public void run() {
			flip();
			// visibleLastIndex - visibleItemCount + 1
			button3.setText("下一页"); // 恢复按钮文字
		    }
		}, 1000);

	    }
	});
	button2.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View v) {

		// TODO Auto-generated method stub
		button2.setText("正在加载..."); // 设置按钮文字loading
		page--;
		if (page <= 0) {
		    page = 1;
		    button2.setText("上一页");
		    return;
		}
		handler.postDelayed(new Runnable() {
		    @Override
		    public void run() {
			flip();
			// visibleLastIndex - visibleItemCount + 1
			button2.setText("上一页"); // 恢复按钮文字
		    }
		}, 1000);

	    }
	});
	textview = (TextView) loadMoreView.findViewById(R.id.listview_info);
	edit = (EditText) loadMoreView.findViewById(R.id.listview_pagenumber);

	loadMoreButton.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View v) {
		
		loadMoreButton.setText("正在加载..."); // 设置按钮文字loading
		String s = edit.getEditableText().toString();
		if (TextUtils.isEmpty(s)) {
		    loadMoreButton.setText("确定");
		    return;
		}
		page = Integer.parseInt(s);
		if (page <= 0) {
		    showMessage("页数不能小于1！！！");
		    loadMoreButton.setText("确定");
		    return;
		}
		if (page >= count) {
		    page = count;
		}
		handler.postDelayed(new Runnable() {
		    @Override
		    public void run() {
			flip();
			// visibleLastIndex - visibleItemCount + 1
			loadMoreButton.setText("确定"); // 恢复按钮文字
		    }
		}, 1000);
	    }
	});
	listView = (ListView) this.findViewById(R.id.listView1);

	listView.addFooterView(loadMoreView); // 设置列表底部视图
	// listView.addHeaderView(v) //设置列表顶部视图
	adapter = new ListViewAdapter(items, this);
	listView.setAdapter(adapter); // 自动为id是list的ListView设置适配器

	listView.setOnScrollListener(this); // 添加滑动监听

	// initAdapter();
	listView.setOnItemClickListener(new OnItemClickListener() {

	    @Override
	    public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {

	    }
	});

	queryDater();
    }

    /**
     * 滑动时被调用
     */
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
	this.visibleItemCount = visibleItemCount;
	visibleLastIndex = firstVisibleItem + visibleItemCount - 1;
    }

    /**
     * 滑动状态改变时被调用
     */
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
	int itemsLastIndex = adapter.getCount() - 1; // 数据集最后一项的索引
	int lastIndex = itemsLastIndex + 1; // 加上底部的loadMoreView项
	if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && visibleLastIndex == lastIndex) {
	    // 如果是自动加载,可以在这里放置异步加载数据的代码
	    Log.i("LOADMORE", "loading...");
	}
    }

    /**
     * 模拟加载数据
     */
    private void loadData() {
	System.out.println(page);
	if (count > 0) {

	} else {
	    list.clear();
	}

	if (page >= count) {
	    if (page != count) {
		showMessage("已到最后一页了");
	    }
	    page = count;

	}

	if (count == 0) {

	    adapter.removeItem();
	    adapter.notifyDataSetChanged(); // 数据集变化后,通知adapter
	    ListViewInfo info = new ListViewInfo();
	    info.setNumber("没有数据..........");
	    items.add(info);
	    listView.setSelection(0); // 设置选中项
	    textview.setText("页数" + page + "/" + count + "   跳转到");
	    list = null;
	    return;
	} else {
	    adapter.removeItem();
	    adapter.notifyDataSetChanged(); // 数据集变化后,通知adapter

	    listView.setSelection(0); // 设置选中项
	    textview.setText("页数" + page + "/" + count + "   跳转到");

	    if (count == 0) {
		Toast.makeText(RushRepairListView.this, "没有查到数据", 1000).show();
		ListViewInfo info = new ListViewInfo();
		info.setNumber("没有数据..........");
		items.add(info);
		list = null;
		return;
	    }
	    if (list == null || list.size() < 1) {

		return;
	    }
	    for (int i = 0; i < list.size(); i++) {
		RepairVo use = list.get(i);
		DeviceVo device=use.getDevice();
		ListViewInfo info = new ListViewInfo();
		info.setNumber(i + 1 + (page - 1) * 10 + "");
		info.setUserinfo("设备编号:" + device.getDeviceNumber()+"  地址:"+device.getDeviceAdds());
		info.setWaterinfo("强修方式:" + use.getRepairmethod());
		
		items.add(info);
	    }
	}

    }

    /**
     * 查询数据
     */
    public void queryDater() {
	count = 0;
	page = 1;
	model = new Model(page, 10);
	
	list=dao.queryModelDevice(model);
	count=dao.queryPageCount();
	loadData();
    }

    /**
     * 翻页
     */
    public void flip() {
	
	
	model = new Model(page, 10);
	list=dao.queryModelDevice(model);
	count=dao.queryPageCount();
	
	loadData();
    }

    // 提示消息
    public void showMessage(String message) {
	Toast toast = Toast.makeText(RushRepairListView.this, message, Toast.LENGTH_SHORT);
	toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 200);
	toast.show();
    }
    @Override
    protected void onDestroy() {
	dao.close();
	super.onDestroy();
    }

    public ListView getListView() {
        return listView;
    }

    public void setListView(ListView listView) {
        this.listView = listView;
    }
    
    
}
